/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.test.support;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;

@RequiredArgsConstructor
public class LogableCache implements Cache {
    private static final Logger log = LoggerFactory.getLogger("cache.TRACE");

    @NonNull @Getter private final Cache cache;

    @Override
    public String getName() {
        return cache.getName();
    }

    @Override
    public Object getNativeCache() {
        return cache.getNativeCache();
    }

    @Override
    public ValueWrapper get(Object key) {
        ValueWrapper valueWrapper = cache.get(key);
        if (valueWrapper != null) {
            log.debug("Cache hit, return {} for key {} in {}", valueWrapper.get(), key, getName());
        } else {
            log.debug("Cache miss, key {} not found in {}", key, getName());
        }
        return valueWrapper;
    }

    @Override
    public void put(Object key, Object value) {
        cache.put(key, value);
        log.debug("Put {} under key {} into {}", value, key, getName());
    }

    @Override
    public void evict(Object key) {
        cache.evict(key);
        log.debug("Evict key {} from cache {}", key, getName());
    }

    @Override
    public void clear() {
        cache.clear();
        log.debug("Clear cache {}", getName());
    }
}
